// pages/activityDetail/activityDetail.js
const WxParse = require('../../../wxParse/wxParse.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        detail: null,

        isShowCount: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.goodsId = options.id;
        this.onPullDownRefresh();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.loadDetail().finally(() => {
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        const image = "";
        return {
            imageUrl: image,
            path: this.route + `?id=${this.data.goodsId}`
        }
    },

    onShow: function () {
        if (this.data.isShowCount) {
            this.loadDetail();
        }

        this.data.isShowCount++;
    },

    // 加载商品详情
    loadDetail: function() {
        return wx.http.get('/api/activity/detail', {
            goods_id: this.data.goodsId
        }).then((res) => {
            const detail = res.data.data.detail;
            detail.isEnded = parseInt(new Date().getTime() / 1000) > detail.activity_end_time.value;
            this.setData({
                detail: detail,
            }, () => {
                WxParse.wxParse('detailContent', 'html', detail.activity_content, this);
            });
            return detail;
        }, (err) => {
            console.warn("/api/activity/detail", err)
        });
    },

    // 收藏
    collectionTap: function() {
        return wx.http.post('api/activity/collect', {
            goods_id: this.data.goodsId,
        }).then((res) => {
            const isCollect = !this.data.detail.isCollect;
            this.setData({
                'detail.isCollect': isCollect
            });
            return res.data.data;
        }, (err) => {
            console.warn("api/activity/collect", err)
        });
    },

});